package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhouchao.bean.Goods;
import com.zhouchao.bean.Visit;
import com.zhouchao.dao.GoodsDao;
import com.zhouchao.dao.VisitDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.TimeUtil;
/**
 * 访客登记
 * 
 * @author 周超
 *
 */
public class VisitServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		DbUtil dbUtil = new DbUtil();
		VisitDao gd=new VisitDao();
		String action = request.getParameter("action");
		Connection con = null;
	if("into".equals(action)){
		request.setAttribute("time", tm);
		request.setAttribute("mainPage", "dormManager/visit.jsp");
		request.getRequestDispatcher("mainManager.jsp").forward(request, response);
	}else if("all".equals(action)){
		try {
			con=dbUtil.getCon();
			List<Visit> visitlist=new ArrayList<Visit>();
			visitlist=gd.visitList(con);
			request.setAttribute("visitlist", visitlist);
			request.setAttribute("mainPage", "dormManager/visitlist.jsp");
			request.getRequestDispatcher("mainManager.jsp").forward(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	else{
		System.out.println("22222222222222222222222222");
		String buildid1 = request.getParameter("buildid");
		String buildid=new String(buildid1.getBytes("ISO-8859-1"),"utf-8");
		String dorm = request.getParameter("dorm");
		String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
		String tel = request.getParameter("tel");
		String stuname=new String(request.getParameter("stuname").getBytes("ISO-8859-1"),"utf-8");
		String stunum = request.getParameter("stunum");
		String date = request.getParameter("date");
		String tt=new String(request.getParameter("tt").getBytes("ISO-8859-1"),"utf-8");
		Visit gs=new Visit(name,tel,stuname,stunum,date,tt);
		System.out.println(gs);
	
		try {
			con=dbUtil.getCon();
			gd.intovisit(con, gs);
			request.setAttribute("time", tm);
			request.setAttribute("mainPage", "dormManager/visit.jsp");
			request.getRequestDispatcher("mainManager.jsp").forward(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
}
	}

}
